#coding=utf-8
#!/usr/bin/python

'''
Created on 2012-11-17

@author: Neo.Chen
'''

'''
功能：
'''

import random
import urllib.request
from urllib import parse
import json

class SimSimi:
    def __init__(self):
        pass

    def chat(self, message):
        params = parse.quote(message)
        myurl = 'http://www.simsimi.com/func/req?msg=%s&lc=ch' % (params)
        req = urllib.request.Request(myurl)
        req.add_header('Referer', 'http://www.simsimi.com/talk.htm?lc=ch')
        req.add_header('X-Requested-With', 'XMLHttpRequest')
        req.add_header('Content-Type', 'application/json; charset=utf-8')
        req.add_header('Accept', 'application/json, text/javascript, */*; q=0.01')
        req.add_header('Cookie', 'JSESSIONID=255E28BBF642813DFB73271C57AB4F98; AWSELB=15E16D030EBAAAB8ACF4BD9BB7E0CA8FB501388662640BCEC6E9C54E70B150AA8514D30E844A0F6781F3C00BEC43069730243F418119D4A1660F073D105DD873991975B881; sagree=true; __utma=119922954.1005283846.1373945805.1373945805.1373945805.1; __utmb=119922954.3.9.1373945851999; __utmc=119922954; __utmz=119922954.1373945805.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)')
        response = urllib.request.urlopen(req)
        data = response.read()
        strdata = bytes.decode(data)
        print(strdata)
        try:
            locations = json.loads(strdata)
            answer = locations["response"]
        except:
            answer = random.choice(['呵呵', '。。。', '= =', '=。='])
        return answer

def main():
    sim = SimSimi()
    ret = sim.chat("你去死")
    print(ret)

if __name__ == '__main__':
    main()

